LoginRequest.validate   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 10
c 0
b 0
f 0
rs 10
cc 2
1
import { schema, rules as schemaRules, validator } from '@ioc:Adonis/Core/Validator'
2
import { RequestContract } from '@ioc:Adonis/Core/Request'
3
4
const AUTHORIZE = (): boolean => {
5
  return true
6
}
7
8
const RULES = (): any => {
9
  return schema.create({
10
    email: schema.string({ trim: true }, [
11
      schemaRules.email(),
12
      schemaRules.exists({ table: 'users', column: 'email' })
13
    ]),
14
    password: schema.string({ trim: true }, [
15
      schemaRules.minLength(8),
16
      schemaRules.maxLength(150)
17
    ]),
18
  })
19
}
20
21
export default class LoginRequest {
22
  public static validate = async (request: RequestContract): Promise<any> => {
23
    if (AUTHORIZE() === false) {
24
      return {
25
        errors: 'Unauthorized'
26
      }
27
    }
28
29
    return await request.validate({
30
      schema: RULES(),
31
      reporter: validator.reporters.api, //For APIs
32
    })
33
  }
34
}
35